#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
/*
This is the corresponding sketch to the 'basicSend' sketch.
 the nrf24l01 will listen for numbers 0-255, and light the red LED
 whenever a number in the sequence is missed.  Otherwise,
 it lights the green LED
 */
int msg[1];
RF24 radio(48 ,53);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int lastmsg = 1;
int enable = 13;
int m1 = 12;
int m2 = 11;
int enable2 = 10;
int m3 = 9;
int m4 = 8;
int enable3 = 7;
int m5 = 6;
int m6 = 5;
int enable4 = 4;
int m7 = 3;
int m8 = 2;


void setup(void){
  Serial.begin(9600);
  radio.begin();

  radio.openReadingPipe(1,pipe);
  radio.startListening();

  pinMode(enable, OUTPUT);
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(enable2, OUTPUT);
  pinMode(m3, OUTPUT);
  pinMode(m4, OUTPUT);
  pinMode(enable3, OUTPUT);
  pinMode(m5, OUTPUT);
  pinMode(m6, OUTPUT);
  pinMode(enable4, OUTPUT);
  pinMode(m7, OUTPUT);
  pinMode(m8, OUTPUT);

}

void loop(void){
  //Serial.println("Transmitting");
  if (radio.available()){
    bool done = false;  

    while (!done){
      done = radio.read(msg, 2); 
      //Serial.println(done);
      if (msg[0] == 0){
        analogWrite(enable,0); // this sets the speed for the motor at 50
        digitalWrite(m1, LOW);
        digitalWrite(m2, LOW);
        analogWrite(enable2,0); // this sets the speed for the motor at 50
        digitalWrite(m3, LOW);
        digitalWrite(m4, LOW);
        analogWrite(enable3,0); // this sets the speed for the motor at 50
        digitalWrite(m5, LOW);
        digitalWrite(m6, LOW);
        analogWrite(enable4,0); // this sets the speed for the motor at 50
        digitalWrite(m7, LOW);
        digitalWrite(m8, LOW);
      }    
      else if(msg[0] >= 1 && msg[0] <= 25) {
        int motor = 100;
        analogWrite(enable,motor); // this sets the speed for the motor at 50
        digitalWrite(m1, HIGH);
        digitalWrite(m2, LOW);
        analogWrite(enable2,motor); // this sets the speed for the motor at 50
        digitalWrite(m3, HIGH);
        digitalWrite(m4, LOW);
        analogWrite(enable3,motor); // this sets the speed for the motor at 50
        digitalWrite(m5, LOW);
        digitalWrite(m6, HIGH);
        analogWrite(enable4,motor); // this sets the speed for the motor at 50
        digitalWrite(m7, HIGH);
        digitalWrite(m8, LOW);
      }
      else if(msg[0] <= 256 && msg[0] >= 231) {
        int motor = 100;
        analogWrite(enable,motor); // this sets the speed for the motor at 50
        digitalWrite(m1, LOW);
        digitalWrite(m2, HIGH);
        analogWrite(enable2,motor); // this sets the speed for the motor at 50
        digitalWrite(m3, LOW);
        digitalWrite(m4, HIGH);
        analogWrite(enable3,motor); // this sets the speed for the motor at 50W
        digitalWrite(m6, LOW);
        analogWrite(enable4,motor); // this sets the speed for the motor at 50
        digitalWrite(m7, LOW);
        digitalWrite(m8, HIGH);
      }  else if(msg[0] == 72) {
        int motor = 100;
        /*analogWrite(enable,200); // this sets the speed for the motor at 50
        digitalWrite(m1, LOW);
        digitalWrite(m2, HIGH);
        analogWrite(enable2,motor); // this sets the speed for the motor at 50
        digitalWrite(m3, HIGH);
        digitalWrite(m4, LOW);
        analogWrite(enable3,motor); // this sets the speed for the motor at 50
        digitalWrite(m5, LOW);
        digitalWrite(m6, HIGH);
        analogWrite(enable4,200); // this sets the speed for the motor at 50
        digitalWrite(m7, LOW);
        digitalWrite(m8, HIGH);
        
        */
        int motor = 100;
        analogWrite(enable,motor); // this sets the speed for the motor at 50
        digitalWrite(m1, LOW);
        digitalWrite(m2, HIGH);
        analogWrite(enable2,150); // this sets the speed for the motor at 50
        digitalWrite(m3, LOW);
        digitalWrite(m4, HIGH);
        analogWrite(enable3,motor); // this sets the speed for the motor at 50W
        digitalWrite(m6, LOW);
        digitalWrite(m5, HIGH);
        analogWrite(enable4,150); // this sets the speed for the motor at 50
        digitalWrite(m7, LOW);
        digitalWrite(m8, HIGH);

      }
      Serial.println(msg[0]);


    }
  }
  else {
    Serial.println("No signal");

  }
  delay(1000);
}

